<?php
class Application_Model_DbTable_Bet extends Zend_Db_Table_Abstract
{
    protected $_name = 'bet';
	protected $_referenceMap = array(
		'User' => array(
			'columns' => 'user_id',
			'refTableClass' => 'Application_Model_DbTable_User',
			'refColumns' => array('id')
		),
		'Event' => array(
			'columns' => 'event_id',
			'refTableClass' => 'Application_Model_DbTable_Event',
			'refColumns' => array('id')
		)
	);
	public function __construct(){
		$this->setRowClass('App_Db_Row_Bet');
		parent::__construct();
	}
    public function GetBetsById($bet_id = null, $user_id = null, $event_id = null, $limit = null, $closed = true){
    	$select = $this->select();
    	if($bet_id){
    		$select->where('id = ?', (int)$bet_id);
    	}
    	if($user_id){
    		$select->where('user_id = ?', (int)$user_id);
    	}
        if($event_id){
    		$select->where('event_id = ?', (int)$event_id);
    	}
    	if($limit){
    		$select->limit($limit);
    	}
    	if($closed){
    		$select->where('status <> ?', 'closed');
    	}
    	$select->order('date_updated DESC');
    	$res = $this->fetchAll($select);
		if(!$res){
    		throw new Zend_Db_Table_Exception('Cant find bet', null);
    	}
    	return $res;
    }
    public function CheckBetExists($event_id, $user_id){
    	$select = $this->select();
    	$select->where('event_id = ?', $event_id)->where('user_id = ?', $user_id);
    	$res = $this->fetchRow($select);
    	if($res){
    		return $res->id;
    	}
    	else{
    		return 0;
    	}
    }
    public function SaveBet($event_id, $user_id, $bet_left, $bet_right){
    	if($bet_left > $bet_right){
    		$bet_result = 'left';
    	}
    	elseif($bet_left < $bet_right){
    		$bet_result = 'right';
    	}
    	else{
    		$bet_result = 'draw';
    	}
    	$exists = $this->CheckBetExists($event_id, $user_id);
    	$data = array('event_id' => $event_id, 
    					'user_id' => $user_id, 'bet_left' => $bet_left, 
    					'bet_right' => $bet_right, 'bet_result' => $bet_result);
    	if($exists){
    		return $this->update($data, 'id = ' . $exists);
    	}
    	else{
    		return $this->insert($data);
    	}
    }
    public function close($bet, $result, $score_left, $score_right){
    	$points = 0;
    	if($bet->bet_result == $result){
    		$points = 1;
    		if($bet->bet_left == $score_left && $bet->bet_right == $score_right){
    			$points = 3;
    		}
    	}
    	$bet->points = $points;
    	$bet->status = 'closed';
    	$bet->save();
    	//$data = array('points' => $points, 'status' => 'closed');
    }
    public function update($data, $where)
    {
    	$data['date_updated'] = date('Y-m-d H:i:s');
    	parent::update($data, $where);
    }
	public function deleteBet($bet_id){
		if(!$bet_id){
			return 0;
		}
		$where = 'id = ' . $bet_id;
		$this->delete($where);
	}
	public function countPoints($user_id){
		if(!$user_id){
			return 0;
		}
		$bets = $this->GetBetsById(null, $user_id, null, null, false);
		$points = 0;
		foreach ($bets as $bet){
			if($bet->status == 'closed'){
				$points += $bet->points;
			}
		}
		return $points;
	}
}
